//package com.ocom.sentinel;
//
//import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
//import com.alibaba.csp.sentinel.slots.block.BlockException;
//import com.alibaba.fastjson.JSON;
//import io.netty.buffer.UnpooledByteBufAllocator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.NettyDataBufferFactory;
//import org.springframework.http.codec.HttpMessageWriter;
//import org.springframework.http.codec.ServerCodecConfigurer;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//import org.springframework.web.reactive.function.server.ServerResponse;
//import org.springframework.web.reactive.result.view.ViewResolver;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.server.WebExceptionHandler;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.io.UnsupportedEncodingException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.function.Supplier;
//
//
//@Component
//@Primary
//public class JsonSentinelGatewayBlockExceptionHandler implements WebExceptionHandler {
//
//    private List<ViewResolver> viewResolvers;
//    private List<HttpMessageWriter<?>> messageWriters;
//    private final Supplier<ServerResponse.Context> contextSupplier = () -> {
//        return new ServerResponse.Context() {
//            public List<HttpMessageWriter<?>> messageWriters() {
//                return JsonSentinelGatewayBlockExceptionHandler.this.messageWriters;
//            }
//
//            public List<ViewResolver> viewResolvers() {
//                return JsonSentinelGatewayBlockExceptionHandler.this.viewResolvers;
//            }
//        };
//    };
//
//
//    public JsonSentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer) {
//        this.viewResolvers = viewResolvers;
//        this.messageWriters = serverCodecConfigurer.getWriters();
//    }
//
//    @Bean
//    @Primary
//    private Mono<Void> writeResponse(ServerResponse response, ServerWebExchange exchange) {
//        ServerHttpResponse serverHttpResponse = exchange.getResponse();
//        serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
////        JSONObject responseObj = new JSONObject();
////        responseObj.put("code", 403);
////        responseObj.put("msg", "请求过于频繁，请稍后重试");
//
//        Map<String,String> result=new HashMap<String,String>(){{
//            put("code","403");
//            put("msg","请求过于频繁，请稍后重试");
//        }};
//        return serverHttpResponse.writeWith(Flux.create(sink -> {
//            NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(new UnpooledByteBufAllocator(false));
//            try {
//                DataBuffer dataBuffer= nettyDataBufferFactory.wrap(JSON.toJSONString(result).getBytes("utf8"));
//                //DataBuffer dataBuffer= nettyDataBufferFactory.wrap("asdf".getBytes("utf8"));
//                sink.next(dataBuffer);
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//            sink.complete();
//        }));
//
////        byte[] datas = responseObj.toString().getBytes(StandardCharsets.UTF_8);
////        DataBuffer buffer = (DataBuffer) serverHttpResponse.bufferFactory().wrap(datas);
////        return serverHttpResponse.writeWith(Flux.create(sink -> {
////            NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(new UnpooledByteBufAllocator(false));
////            sink.next((org.springframework.core.io.buffer.DataBuffer) buffer);
////            sink.complete();
////        }));
//    }
//
//    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
//        if (exchange.getResponse().isCommitted()) {
//            return Mono.error(ex);
//        } else {
//            return !BlockException.isBlockException(ex) ? Mono.error(ex) : this.handleBlockedRequest(exchange, ex).flatMap((response) -> {
//                return this.writeResponse(response, exchange);
//            });
//        }
//    }
//
//    private Mono<ServerResponse> handleBlockedRequest(ServerWebExchange exchange, Throwable throwable) {
//        return GatewayCallbackManager.getBlockHandler().handleRequest(exchange, throwable);
//    }
//
//}
